package com.best.oasis.settlement.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.best.oasis.settlement.common.vo.customer.CustomerVO;
import com.best.oasis.settlement.common.vo.dealer.DealerVO;

public class CustomerImport {

	public static CustomerVO getCustomter(Connection con, String customerName) throws SQLException{
		CustomerVO vo = null;
		StringBuffer sql = new StringBuffer("select id,code,name from st_customer where name='" + customerName + "'");
		PreparedStatement ps = con.prepareStatement(sql.toString());
		ResultSet rs = ps.executeQuery();
		while(rs.next()){
			vo = new CustomerVO();
			vo.setId(rs.getLong(1));
			vo.setCode(rs.getString(2));
			vo.setName(rs.getString(3));
		}
		rs.close();
		ps.close();
		return vo;
	}
	
	public static DealerVO getDealer(Connection con, String dealerName,Long customer_id) throws SQLException{
		DealerVO vo = null;
		StringBuffer sql = new StringBuffer("select id,code,name,customer_id from st_dealer where name='" + dealerName + "' and customer_id=" + customer_id);
		PreparedStatement ps = con.prepareStatement(sql.toString());
		ResultSet rs = ps.executeQuery();
		while(rs.next()){
			vo = new DealerVO();
			vo.setId(rs.getLong(1));
			vo.setCode(rs.getString(2));
			vo.setName(rs.getString(3));
			CustomerVO customer = new CustomerVO();
			customer.setId(rs.getLong(4));
			vo.setCustomer(customer);
		}
		rs.close();
		ps.close();
		return vo;
	}
	
	public static void updateCustomerBalance(Connection con, Long customerId, Double amout) throws Exception {
		StringBuffer sql = new StringBuffer("update st_customer set REMAINAMOUNT = REMAINAMOUNT + " + amout + " where id=" + customerId);
		PreparedStatement ps = con.prepareStatement(sql.toString());
		if(ps.executeUpdate() != 1)
			throw new Exception("更新数据客户余额出错。");
	}
}
